using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace Wrath.Xml
{
    public class ValidElementsCollection
    {
        private Dictionary<FileType, List<string>> fileTypeStrings;

        public ValidElementsCollection()
        {
            fileTypeStrings = new Dictionary<FileType, List<string>>();
        }

        public List<string> this[FileType fileType]
        {
            get
            {
                return fileTypeStrings[fileType];
            }
            set
            {
                fileTypeStrings[fileType] = value;
            }
        }

        public List<FileType> this[string elementName]
        {
            get
            {
                List<FileType> fileTypeList = new List<FileType>();

                foreach (KeyValuePair<FileType, List<string>> kvp in fileTypeStrings)
                {
                    List<string> stringList = kvp.Value;
                    if (stringList.Contains(elementName.ToLower()))
                    {
                        if (!fileTypeList.Contains(kvp.Key)) 
                        {
                            fileTypeList.Add(kvp.Key);
                        }
                    }
                }

                return fileTypeList;
            }
            set
            {
                List<FileType> fileTypeList = value;

                foreach (FileType fileType in fileTypeList)
                {
                    // TODO: What if fileType isnt in the list??

                    List<string> stringList = fileTypeStrings[fileType];
                    if (!stringList.Contains(elementName.ToLower()))
                    {
                        stringList.Add(elementName.ToLower());
                    }
                }
            }
        }

        public void Add(FileType fileType, string elementName)
        {
            List<string> stringList;

            try
            {
                stringList = fileTypeStrings[fileType];
            }
            catch (KeyNotFoundException)
            {
                stringList = new List<string>();
                fileTypeStrings.Add(fileType, stringList);
            }

            if (!stringList.Contains(elementName.ToLower()))
            {
                stringList.Add(elementName.ToLower());
            }
        }

        public void Add(FileType fileType, List<string> items)
        {
            foreach (string name in items)
            {
                Add(fileType, name.ToLower());
            }
        }

        public void Clear()
        {
            fileTypeStrings.Clear();
        }

        public bool Contains(string item)
        {
            foreach (KeyValuePair<FileType, List<string>> kvp in fileTypeStrings)
            {
                List<string> stringList = kvp.Value;
                if (stringList.Contains(item.ToLower()))
                {
                    return true;
                }
            }

            return false;
        }

        public bool FileTypeContains(FileType fileType, string item)
        {
            List<string> stringList = fileTypeStrings[fileType];
            if (stringList.Contains(item.ToLower()))
            {
                return true;
            }

            return false;
        }

        public FileType GetFileType(List<string> elementNames)
        {
            FileType returnType = FileType.Unknown;

            foreach (KeyValuePair<FileType, List<string>> kvp in fileTypeStrings)
            {
                List<string> stringList = kvp.Value;

                bool broken = false;

                foreach (string name in elementNames)
                {
                    if (!stringList.Contains(name.ToLower()))
                    {
                        broken = true;
                        break;
                    }
                }

                if (broken == false)
                {
                    returnType = kvp.Key;
                    break;
                }
            }

            return returnType;
        }

        public int Count
        {
            get { return fileTypeStrings.Count; }
        }

        public bool Remove(FileType fileType)
        {
            return fileTypeStrings.Remove(fileType);            
        }

        public bool Remove(FileType fileType, string item)
        {
            List<string> stringList = fileTypeStrings[fileType];
            return stringList.Remove(item.ToLower());
        }
    }
}
